import { FormItemRule } from 'element-plus'
import { Arrayable } from 'element-plus/es/utils'
import { $t } from './vue_pbulic'
interface RuleClassType extends Partial<Record<string, Arrayable<FormItemRule>>> {
  [x: string]: Arrayable<FormItemRule> | undefined | any
}
export class RuleClass implements RuleClassType {
  [x: string]: Arrayable<FormItemRule> | undefined | any
  constructor(rules: Array<string>) {
    for (let i = 0; i < rules.length; i++) {
      this[rules[i]] = [{ required: true, message: $t('common.no_enter_rule'), trigger: 'blur' }]
    }
  }
  //删除某个key
  public deleteRules(key: string): void {
    if (this[key]) {
      delete this[key]
    }
  }
  public updateRulesTrigger(key: string, rule_key: keyof FormItemRule, new_data: unknown): void {
    if (this[key]) {
      this[key][rule_key] = new_data
    }
  }
  //修改
  public updateRules(key: string, new_rules: Arrayable<FormItemRule> | undefined): void {
    if (this[key]) {
      this[key] = new_rules
    }
  }
  // 添加
  public addKeyRules(key: string, new_rules: Arrayable<FormItemRule> | undefined): void {
    if (this[key]) {
      this[key].push(new_rules)
    }
  }
}
